﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Web.Mvc;
using NFine.Code;
using NFine.Web.Areas.SSO.Models;
using System.Data.SqlClient;
using NFine.Domain.Entity.SystemManage;

namespace NFine.Web.Areas.SSO.Controllers
{
    public class SubSystemController : ControllerBase
    {
        NFineBaseEntities db = new NFineBaseEntities();

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetList()
        {
            string StrSql = @"
                                                                 select 
                                                                            s.f_id,
                                                                            s.f_user_id,
                                                                            s.f_createtime,
                                                                            s.f_updatetime,
                                                                            s.f_enable,
                                                                            s.f_username,
                                                                            a.Sa_App_Name,
                                                                            u.F_RealName
                                                                from Sso_SubSystem s
                                                                left join Sso_Authtication a
                                                                on s.F_Authentication_Id = a.Sa_Id
                                                                left join Sys_User u
                                                                on s.f_user_id = u.F_Id
            ";
            if (!OperatorProvider.Provider.GetCurrent().IsSystem)
            {
                StrSql += string.Format(" where  u.F_Id ='{0}' ", OperatorProvider.Provider.GetCurrent().UserId);
            }
            var data = db.Database.SqlQuery<SusSystemModel>(StrSql);
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetUserList()
        {
            string StrSql = @"
                                                                SELECT  *
                      FROM [NFineBase].[dbo].[Sys_User]
                      where F_EnabledMark = 1
            ";
            if (!OperatorProvider.Provider.GetCurrent().IsSystem)
            {
                StrSql += string.Format(" and  F_Id ='{0}' ", OperatorProvider.Provider.GetCurrent().UserId);
            }
            var data = db.Database.SqlQuery<UserModel>(StrSql);
            return Content(data.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            //var data = db.Sso_SubSystem.Find(keyValue);
            SqlParameter sqlParameter = new SqlParameter();
            sqlParameter.ParameterName = "@f_id";
            sqlParameter.Value = keyValue;
            var data = db.Database.SqlQuery<SusSystemModel>(@"
                                                                 select 
                                                                            s.f_id,
                                                                            s.f_user_id,
                                                                            s.f_createtime,
                                                                            s.f_updatetime,
                                                                            s.f_enable,
                                                                            s.f_username,
                                                                            s.f_password,
                                                                            a.Sa_App_Name,
                                                                            s.F_Authentication_Id,
                                                                            u.F_RealName
                                                                from Sso_SubSystem s
                                                                left join Sso_Authtication a
                                                                on s.F_Authentication_Id = a.Sa_Id
                                                                left join Sys_User u
                                                                on s.f_user_id = u.F_Id
                                                                where s.f_id = @f_id
            ", sqlParameter).FirstOrDefaultAsync().Result;
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult Add(SusSystemModel susSystemModel, string keyValue)
        {
            if (susSystemModel != null)
            {
                if (string.IsNullOrWhiteSpace(susSystemModel.F_Id))
                {
                    Sso_SubSystem sso_SubSystem = new Sso_SubSystem();
                    sso_SubSystem.F_Id = Guid.NewGuid().ToString("N");
                    sso_SubSystem.F_User_Id = susSystemModel.F_User_Id;
                    sso_SubSystem.F_UserName = susSystemModel.F_Username;
                    sso_SubSystem.F_Password = susSystemModel.F_Password;
                    sso_SubSystem.F_Authentication_Id = susSystemModel.F_Authentication_Id;
                    sso_SubSystem.F_Enable = susSystemModel.F_Enable;
                    sso_SubSystem.F_CreateTime = DateTime.Now;
                    sso_SubSystem.F_UpdateTime = DateTime.Now;
                    db.Sso_SubSystem.Add(sso_SubSystem);
                    db.SaveChangesAsync();
                    return Success("添加成功。");
                }
                else
                {
                    Sso_SubSystem sso_SubSystem = new Sso_SubSystem();
                    sso_SubSystem.F_Id = susSystemModel.F_Id;
                    sso_SubSystem.F_User_Id = susSystemModel.F_User_Id;
                    sso_SubSystem.F_UserName = susSystemModel.F_Username;
                    sso_SubSystem.F_Password = susSystemModel.F_Password;
                    sso_SubSystem.F_Authentication_Id = susSystemModel.F_Authentication_Id;
                    sso_SubSystem.F_Enable = susSystemModel.F_Enable;
                    sso_SubSystem.F_UpdateTime = DateTime.Now;
                    db.Sso_SubSystem.Add(sso_SubSystem);
                    db.Entry(sso_SubSystem).State = EntityState.Modified;
                    db.SaveChangesAsync();
                    return Success("修改成功。");
                }

            }
            else
            {
                return Error("操作失败");
            }
        }

    }
}
